package com.rich.web.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rich.dao.ProductDao;
import com.rich.domain.PageBean;
import com.rich.domain.Product;
import com.rich.service.ProductServer;
import com.rich.service.impl.ProductServerImpl;
import com.rich.utils.BeanFactory;

@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {

	ProductServer ps = (ProductServer) BeanFactory.getBean("ProductService");
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 根据商品id获取详细信息,显示到info.jsp
	 */
	public String getById(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取pid
			String pid = request.getParameter("pid");
			//获取商品信息
			Product product = ps.getById(pid);
			//信息放到域对象中
			request.setAttribute("bean", product);
		} catch (Exception e) {
			request.setAttribute("msg", "获取信息失败，请重新加载！");
		}
		return "/jsp/product_info.jsp";
	}

	/**
	 * 分类商品分页展示
	 */
	public String findByPage(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取pageNumber、cId，设置pageSize
			int pageNumber = 1;
			pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
			int pageSize = 12;
			String cid = request.getParameter("cid");
			//调用server分页查询商品
			PageBean<Product> bean = ps.findByPage(pageNumber,pageSize,cid);
			//把pageBean放入域中
			request.setAttribute("pb",bean);
		} catch (Exception e) {
			request.setAttribute("msg", "分页查询失败！");
			return "/jsp/msg.jsp";
		}
		return "/jsp/product_list.jsp";
	}
}
